package com.example.votemanage.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.votemanage.config.ResponseData;
import com.example.votemanage.config.Result;
import com.example.votemanage.entity.Auction;
import com.example.votemanage.entity.AuctionRecord;
import com.example.votemanage.entity.AuctionUser;
import com.example.votemanage.service.AuctionService;
import com.example.votemanage.service.Impl.AuctionServiceImpl;
import com.example.votemanage.service.Impl.AuctionUserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class AuctionController {
    @Autowired
    AuctionServiceImpl auctionService;
    @GetMapping("/index")
    public Page<Auction> getAuctionRecords(@RequestParam(defaultValue = "1") int page,
                                           @RequestParam(defaultValue = "10") int size) {
        return auctionService.page(new Page<>(page, size));
    }
    @GetMapping("/selectPage")
    public Page<Auction> getAuctionPage(
            @RequestParam(defaultValue = "1") int pageIndex,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam Map<String, Object> params) {
        Page<Auction> page = new Page<>(pageIndex, pageSize);
        return auctionService.pageWithConditions(page, params);
    }
    @GetMapping("/selectById")
    public Auction getAuctionById(@RequestParam int auctionId) {
        return auctionService.getById(auctionId);
    }
    @PutMapping("/update")
    public Result update(@RequestBody Auction auction) {
        return Result.success(auctionService.updateById(auction));
    }
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        Boolean success = auctionService.removeById(id);
        return success ? Result.success() : Result.success("Failed to delete the auction with id: " + id);
    }
    @PostMapping("/insert")
    public ResponseEntity<?> insert(@RequestBody Auction user) {
        try {
            boolean result = auctionService.save(user);
            System.out.println("result = " + result);
            if (result) {
                return ResponseEntity.ok(new ResponseData("200", "Update successful", user));
            } else {
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                        .body(new ResponseData("500", "Update failed", null));
            }
        } catch (Exception e) {
            // 打印日志
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(new ResponseData("500", "Internal server error", e.getMessage()));
        }
    }

}
